public class ThreadDemo14task2 {
    public static int i = 1;

    public static void main(String[] args) {
        Object lock1 = new Object();
        Object lock2 = new Object();
        Object lock3 = new Object();

        Object lock4 = new Object();
        Object lock5 = new Object();
        Object lock6 = new Object();

        Thread A = new Thread(()->{

            while(i<=10){
                //改法二：
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (lock1){

                    System.out.print("A ");

                    lock1.notify();
                }
                synchronized (lock3){
                    lock3.notify();
                }



                synchronized (lock4){
                    try {
                        lock4.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });

        Thread B = new Thread(()->{
            while(i<=10){
                synchronized (lock1){
                    try {
                        lock1.wait();
                        System.out.print("B ");
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

                }
                synchronized (lock2){
                    lock2.notify();
                }

            }
        });

        Thread C = new Thread(()->{
            while(i<=10){
                synchronized (lock2){
                    try {
                        lock2.wait();
                        System.out.println("C ");
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

                }
                synchronized (lock4){
                    i++;
                    lock4.notify();
                }
            }
        });

        A.start();
        B.start();
        C.start();
    }
    public static void main1(String[] args) {

        //int i = 1;

        Object lock1 = new Object();
        Object lock2 = new Object();
        Object lock3 = new Object();

        Object lock4 = new Object();
        Object lock5 = new Object();
        Object lock6 = new Object();

        Thread A = new Thread(()->{
            while(i<=10){
                synchronized (lock1){
                    System.out.println("A ");

                    //lock1.notify();
                }

                //改法一：lock3控制B线程
                synchronized (lock3){
                    lock3.notify();
                }



                synchronized (lock4){
                    try {
                        lock4.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });

        Thread B = new Thread(()->{
            while(i<=10){
                synchronized (lock3){
                    try {
                        lock3.wait();
                        System.out.println("B ");
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

                }
                synchronized (lock2){
                    lock2.notify();
                }

            }
        });

        Thread C = new Thread(()->{
            while(i<=10){
                synchronized (lock2){
                    try {
                        lock2.wait();
                        System.out.println("C ");
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

                }
                synchronized (lock4){
                    i++;
                    lock4.notify();
                }
            }
        });

        A.start();
        B.start();
        C.start();
    }
}
